﻿using CommonServiceLocator;
using OneOfAll.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OneOfAll.View
{
    /// <summary>
    /// BattleView.xaml 的交互逻辑
    /// </summary>
    public partial class BattleView : UserControl
    {
        BattleViewModel viewModel = ServiceLocator.Current.GetInstance<BattleViewModel>();
        double mouseWheelStep = 1.2;
        public BattleView()
        {
            InitializeComponent();
        }

        private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (viewModel == null) return;
            var mousePoint = e.GetPosition(sender as Canvas);
            if (e.Delta > 0)
            {
                if (viewModel.ScalueValue * mouseWheelStep < 5)
                {
                    viewModel.ScalueValue *= mouseWheelStep;
                    double px = mousePoint.X - viewModel.CameraX;
                    double py = mousePoint.Y - viewModel.CameraY;
                    double px_add = px * (mouseWheelStep - 1.0);
                    double py_add = py * (mouseWheelStep - 1.0);
                    viewModel.CameraX -= px_add;
                    viewModel.CameraY -= py_add;
                }
            }
            else
            {
                if (viewModel.ScalueValue / mouseWheelStep > 0.2)
                {
                    viewModel.ScalueValue /= mouseWheelStep;
                    double px = mousePoint.X - viewModel.CameraX;
                    double py = mousePoint.Y - viewModel.CameraY;
                    double px_add = px * (1.0 - 1.0 / mouseWheelStep);
                    double py_add = py * (1.0 - 1.0 / mouseWheelStep);
                    viewModel.CameraX += px_add;
                    viewModel.CameraY += py_add;
                }
            }
        }
    }
}
